package cn.jishiyu11.xeqb.utils

import android.content.Context
import android.graphics.Bitmap
import android.graphics.BitmapFactory
import android.util.SparseArray
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.TextView

import java.io.BufferedInputStream
import java.io.IOException
import java.io.InputStream
import java.net.HttpURLConnection
import java.net.MalformedURLException
import java.net.URL

/**
 * 自己编写的万能ViewHolder
 * by jsy_zj
 */
class ViewHolder(context: Context, parent: ViewGroup, position: Int, layoutId: Int) {
    private val mViwes: SparseArray<View>
    /**
     * 获得position
     */
    var position: Int = 0
        private set
    val convertView: View

    init {
        this.mViwes = SparseArray()
        this.position = position
        convertView = LayoutInflater.from(context).inflate(layoutId, parent, false)
        convertView.tag = this
    }

    /**
     * 通过viewId获取控件,输入 R.id...
     * @param viewId
     * @return  View
     */
    fun <T : View> getView(viewId: Int): T {
        var view: View? = mViwes.get(viewId)
        if (view == null) {
            view = convertView.findViewById(viewId)
            mViwes.put(viewId, view)
        }
        return (view as T?)!!
    }

    /**
     * 设置TextView内容,输入 R.id..., 和  data.gettext.
     * @param int viewId,String text
     * @return ViewHolder
     */
    fun setText(viewId: Int, text: String): ViewHolder {
        val tv = getView<TextView>(viewId)
        tv.text = text
        return this
    }

    /**
     * 设置ImageView内容,输入 R.id..., 和  res文件夹.
     * @param int viewId ,res文件
     * @return ViewHolder
     */
    fun setImageResource(viewId: Int, resId: Int): ViewHolder {
        val iv = getView<ImageView>(viewId)
        iv.setImageResource(resId)
        return this
    }

    /**
     * 设置TextView内容,输入 R.id..., 和  data.gettext.
     * @param int viewId,String text
     * @return ViewHolder
     */
    fun getTextview(viewId: Int): TextView {

        return getView(viewId)
    }


    /**
     * 设置ImageView内容,输入 R.id..., 和  Bitmap .
     * @param int viewId
     * @return ViewHolder
     */
    fun setImageBitamap(viewId: Int, bitmap: Bitmap): ViewHolder {
        val iv = getView<ImageView>(viewId)
        iv.setImageBitmap(bitmap)
        return this
    }

    fun setImageURI(viewId: Int, url: String): ViewHolder {
        val iv = getView<ImageView>(viewId)
        val bm = getImageContent(url)
        iv.setImageBitmap(bm)
        return this
    }

    companion object {
        operator fun get(context: Context, parent: ViewGroup, position: Int, layoutId: Int, convertView: View?): ViewHolder {
            if (convertView == null) {

                return ViewHolder(context, parent, position, layoutId)
            } else {
                val holder = convertView.tag as ViewHolder
                holder.position = position
                return holder
            }
        }

        fun getImageContent(params: String): Bitmap? {
            //获取网络图片
            val url: URL
            var bm: Bitmap? = null
            try {
                url = URL(params)
                val connection = url.openConnection() as HttpURLConnection
                val `is` = connection.inputStream
                val bis = BufferedInputStream(`is`)
                //能够把输入字节流转化为bitmap对象。
                //bitmap-->位图，可以直接显示在imageview当中
                bm = BitmapFactory.decodeStream(bis)
                //关闭流，最好写在finally当中
                `is`.close()
                bis.close()
            } catch (e: MalformedURLException) {
                e.printStackTrace()
            } catch (e: IOException) {
                e.printStackTrace()
            }

            return bm
        }
    }
}
